// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import { ElMessage } from 'element-plus';

import LoginView from '../views/auth/LoginView.vue';
import RegisterView from '../views/auth/RegisterView.vue';

import FrontFrame from '../views/FrontFrame.vue';
import AdminFrame from '../views/AdminFrame.vue';

import NewsView from '@/views/front/NewsView.vue';
import NewsDetail from '../views/pages/NewsDetail.vue';
import AnnounceView from '@/views/front/AnnounceView.vue';
import PersonalCenterView from '@/views/front/PersonalCenterView.vue';
import SearchResults from '@/views/front/SearchResults.vue';

import AnnouncementManage from '@/views/pages/AnnouncementManage.vue';
import NewsManage from '@/views/pages/NewsManage.vue';
import DepartmentManage from '@/views/pages/DepartmentManage.vue';
import VehicleManage from '@/views/pages/VehicleManage.vue';
import EmployeeManage from '@/views/pages/EmployeeManage.vue';
import MaterialTypeManage from '@/views/pages/MaterialTypeManage.vue';
import MaterialManage from '@/views/pages/MaterialManage.vue';
import MaterialInboundManage from '@/views/pages/MaterialInboundManage.vue';
import MaterialIOutboundManage from '@/views/pages/MaterialIOutboundManage.vue';
import FeedbackManage from '@/views/pages/FeedbackManage.vue';
import ApplicationManage from '@/views/pages/ApplicationManage.vue';
import TransportationManage from '@/views/pages/TransportationManage.vue';
import PermissionManage from '@/views/pages/PermissionManage.vue';

const routes = [
  { path: '/', redirect: '/front' },
  { path: '/front', name: 'FrontFrame', component: FrontFrame },
  { path: '/admin', name: 'AdminFrame', component: AdminFrame },

  { path: '/login', name: 'LoginView', component: LoginView },
  { path: '/register', name: 'RegisterView', component: RegisterView },

  { path: '/news', name: 'NewsView', component: NewsView },
  { path: '/news/:id', name: 'NewsDetail', component: NewsDetail },
  { path: '/announcement', name: 'AnnounceView', component: AnnounceView },
  { path: '/personal-center', name: 'PersonalCenterView', component: PersonalCenterView },
  { path: '/search', name: 'SearchResults', component: SearchResults },

  { path: '/announcement-manage', name: 'AnnouncementManage', component: AnnouncementManage },
  { path: '/news-manage', name: 'NewsManage', component: NewsManage },
  { path: '/department-manage', name: 'DepartmentManage', component: DepartmentManage },
  { path: '/vehicle-manage', name: 'VehicleManage', component: VehicleManage },
  { path: '/employee-manage', name: 'EmployeeManage', component: EmployeeManage },
  { path: '/material-type-manage', name: 'MaterialTypeManage', component: MaterialTypeManage },
  { path: '/material-manage', name: 'MaterialManage', component: MaterialManage },
  { path: '/material-inbound-manage', name: 'MaterialInboundManage', component: MaterialInboundManage },
  { path: '/material-outbound-manage', name: 'MaterialIOutboundManage', component: MaterialIOutboundManage },
  { path: '/feedback-manage', name: 'FeedbackManage', component: FeedbackManage },
  { path: '/application-manage', name: 'ApplicationManage', component: ApplicationManage },
  { path: '/transportation-manage', name: 'TransportationManage', component: TransportationManage },
  { path: '/permission-manage', name: 'PermissionManage', component: PermissionManage },
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

router.beforeEach(async (to, from, next) => {
  const { useUserStore } = await import('@/stores/user');
  const { usePathPermissionStore } = await import('@/stores/project/permission');

  const userStore = useUserStore();
  const permissionStore = usePathPermissionStore();

  const userRole = userStore.user?.role;
  const restrictedPaths = permissionStore.getForbiddenPaths(userRole || '');

  // 用户不存在则重定向到登录
  if (!userRole && !permissionStore.publicPaths.includes(to.path)) {
    logRouteGuard(to, userStore, permissionStore, '未登录，重定向到登录页');
    next('/login');
    return;
  }

  if (!restrictedPaths) {
    logRouteGuard(to, userStore, permissionStore, '权限信息丢失');
    ElMessage.error('权限信息丢失，请刷新网页');
    next(false);
    return;
  }

  // 检查路径是否在受限路径中
  if (restrictedPaths.some(path => to.path.startsWith(path))) {
    logRouteGuard(to, userStore, permissionStore, '权限不足');
    ElMessage.error('权限不足，无法访问该页面');
    next(false);
    return;
  }

  next();
});

// 记录路由守卫的拦截信息
function logRouteGuard(to, userStore, permissionStore, reason) {
  const userRole = userStore.user?.role;
  console.log('路由守卫拦截:', {
    to: to.path,
    reason,
    user: userStore.user,
    restrictedPaths: permissionStore.getForbiddenPaths(userRole || ''),
  });
}

export default router;